<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"blog.hory-ai.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","Muse | Mist":320,"display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="服务器三台  按顺序执行123192.168.1.125192.168.1.126192.168.1.127 创建目录在每台机器上操作此步骤">
<meta property="og:type" content="article">
<meta property="og:title" content="多机版———docker-compose搭建MongoDB 3.4 分片集群详细步骤">
<meta property="og:url" content="http://blog.hory-ai.com/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/index.html">
<meta property="og:site_name" content="Horysk 宏睿时空">
<meta property="og:description" content="服务器三台  按顺序执行123192.168.1.125192.168.1.126192.168.1.127 创建目录在每台机器上操作此步骤">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-07-30T17:52:25.000Z">
<meta property="article:modified_time" content="2020-07-30T17:52:25.000Z">
<meta property="article:author" content="Hory Skone">
<meta property="article:tag" content="docker">
<meta property="article:tag" content="mongo">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://blog.hory-ai.com/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>多机版———docker-compose搭建MongoDB 3.4 分片集群详细步骤 | Horysk 宏睿时空</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Horysk 宏睿时空</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horysk" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://blog.hory-ai.com/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Hory Skone">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Horysk 宏睿时空">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          多机版———docker-compose搭建MongoDB 3.4 分片集群详细步骤
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-07-30 17:52:25" itemprop="dateCreated datePublished" datetime="2020-07-30T17:52:25+00:00">2020-07-30</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/mongo/" itemprop="url" rel="index"><span itemprop="name">mongo</span></a>
                </span>
                  , 
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/mongo/docker/" itemprop="url" rel="index"><span itemprop="name">docker</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>12k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>11 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="服务器三台-按顺序执行"><a href="#服务器三台-按顺序执行" class="headerlink" title="服务器三台  按顺序执行"></a>服务器三台  按顺序执行</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">192.168</span><span class="number">.1</span><span class="number">.125</span></span><br><span class="line"><span class="number">192.168</span><span class="number">.1</span><span class="number">.126</span></span><br><span class="line"><span class="number">192.168</span><span class="number">.1</span><span class="number">.127</span></span><br></pre></td></tr></table></figure>
<h2 id="创建目录"><a href="#创建目录" class="headerlink" title="创建目录"></a>创建目录</h2><p>在每台机器上操作此步骤</p>
<a id="more"></a>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /home/seeyii</span><br><span class="line">mkdir mongoCluster</span><br><span class="line">cd mongoCluster</span><br></pre></td></tr></table></figure>
<h3 id="vi-mongod-conf"><a href="#vi-mongod-conf" class="headerlink" title="vi mongod.conf"></a>vi mongod.conf</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">storage:</span><br><span class="line">  dbPath: /data/db</span><br><span class="line">  journal:</span><br><span class="line">    enabled: true</span><br><span class="line">systemLog:</span><br><span class="line">  destination: file</span><br><span class="line">  logAppend: true</span><br><span class="line">  path: /var/log/mongodb/mongod.log</span><br><span class="line"></span><br><span class="line">net:</span><br><span class="line">  maxIncomingConnections: <span class="number">10000</span></span><br><span class="line">replication:</span><br><span class="line">  oplogSizeMB: <span class="number">10240</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># security:</span></span><br><span class="line">  <span class="comment"># keyFile: /data/mongodb/key.file</span></span><br><span class="line">  <span class="comment"># authorization: enabled</span></span><br></pre></td></tr></table></figure>
<h3 id="vi-mongos-conf"><a href="#vi-mongos-conf" class="headerlink" title="vi mongos.conf"></a>vi mongos.conf</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">storage:</span><br><span class="line">  dbPath: /data/db</span><br><span class="line">  journal:</span><br><span class="line">    enabled: true</span><br><span class="line">systemLog:</span><br><span class="line">  destination: file</span><br><span class="line">  logAppend: true</span><br><span class="line">  path: /var/log/mongodb/mongos.log</span><br><span class="line"></span><br><span class="line">net:</span><br><span class="line">  maxIncomingConnections: <span class="number">10000</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># security:</span></span><br><span class="line">  <span class="comment"># keyFile: /data/mongodb/key.file</span></span><br></pre></td></tr></table></figure>
<h2 id="创建目录-1"><a href="#创建目录-1" class="headerlink" title="创建目录"></a>创建目录</h2><h3 id="vi-first-mkdir-sh"><a href="#vi-first-mkdir-sh" class="headerlink" title="vi first_mkdir.sh"></a>vi first_mkdir.sh</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p /database/vol/conf/config</span><br><span class="line">mkdir -p /database/vol/conf/db</span><br><span class="line">mkdir -p /database/vol/shard1/config</span><br><span class="line">mkdir -p /database/vol/shard1/db</span><br><span class="line">mkdir -p /database/vol/shard1/backup</span><br><span class="line">mkdir -p /database/vol/shard2/config</span><br><span class="line">mkdir -p /database/vol/shard2/db</span><br><span class="line">mkdir -p /database/vol/shard2/backup</span><br><span class="line">mkdir -p /database/vol/mongos/config</span><br><span class="line">mkdir -p /database/vol/mongos/db</span><br></pre></td></tr></table></figure>
<h3 id="生成认证文件-root-用户-三台共用一个"><a href="#生成认证文件-root-用户-三台共用一个" class="headerlink" title="生成认证文件 root 用户 三台共用一个"></a>生成认证文件 root 用户 三台共用一个</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">openssl rand -base64 <span class="number">741</span> &gt; key.file</span><br><span class="line">chmod <span class="number">600</span> key.file</span><br><span class="line">chown <span class="number">999</span> key.file</span><br><span class="line">mv key.file /database/vol</span><br></pre></td></tr></table></figure>


<h3 id="将配置文件放到指定位置"><a href="#将配置文件放到指定位置" class="headerlink" title="将配置文件放到指定位置"></a>将配置文件放到指定位置</h3><p>vi second_mv.sh</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> /database/vol/</span><br><span class="line"> do</span><br><span class="line">   <span class="keyword">for</span> item2 <span class="keyword">in</span> `ls $item`</span><br><span class="line">    do</span><br><span class="line">     <span class="keyword">if</span> [ $item2 = <span class="string">&#x27;mongos&#x27;</span> ]</span><br><span class="line">     then</span><br><span class="line">        echo $item2</span><br><span class="line">        cp mongos.conf $item$item2/config/mongos.conf</span><br><span class="line">     <span class="keyword">else</span></span><br><span class="line">        echo <span class="string">&quot;no&quot;</span></span><br><span class="line">        cp mongod.conf $item$item2/config/mongod.conf</span><br><span class="line">    fi</span><br><span class="line">    done</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h2 id="docker-compose-yaml-容器编排"><a href="#docker-compose-yaml-容器编排" class="headerlink" title="docker-compose.yaml 容器编排"></a>docker-compose.yaml 容器编排</h2><p>将文件放在 mongoCluster目录中</p>
<h2 id="docker-compose-文件"><a href="#docker-compose-文件" class="headerlink" title="docker-compose 文件"></a>docker-compose 文件</h2><p>docker-compose.yaml</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">&#x27;2&#x27;</span></span><br><span class="line">services:</span><br><span class="line">  rs_config_server:</span><br><span class="line">    image: mongo:<span class="number">3.4</span>  <span class="comment">#  可以不用设置版本,注意参考方案二</span></span><br><span class="line">    command: mongod -f /etc/mongod/mongod.conf --configsvr --replSet <span class="string">&quot;rs-config-server&quot;</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /database/vol/key.file:/data/mongodb/key.file</span><br><span class="line">      - /database/vol/conf/config:/etc/mongod</span><br><span class="line">      - /database/vol/conf/db:/data/db</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;10021:27019&quot;</span></span><br><span class="line">    restart:</span><br><span class="line">      always</span><br><span class="line">    container_name:</span><br><span class="line">      rs_config_server</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br><span class="line"></span><br><span class="line">  rs_shard_server1:</span><br><span class="line">    image: mongo:<span class="number">3.4</span></span><br><span class="line">    command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet <span class="string">&quot;rs-shard1-server&quot;</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /database/vol/key.file:/data/mongodb/key.file</span><br><span class="line">      - /database/vol/shard1/config:/etc/mongod</span><br><span class="line">      - /database/vol/shard1/db:/data/db</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;10031:27018&quot;</span></span><br><span class="line">    restart:</span><br><span class="line">      always</span><br><span class="line">    container_name:</span><br><span class="line">      rs_shard_server1</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br><span class="line"></span><br><span class="line">  rs_shard_server2:</span><br><span class="line">    image: mongo:<span class="number">3.4</span></span><br><span class="line">    command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet <span class="string">&quot;rs-shard2-server&quot;</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /database/vol/key.file:/data/mongodb/key.file</span><br><span class="line">      - /database/vol/shard2/config:/etc/mongod</span><br><span class="line">      - /database/vol/shard2/db:/data/db</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;10041:27018&quot;</span></span><br><span class="line">    restart:</span><br><span class="line">      always</span><br><span class="line">    container_name:</span><br><span class="line">      rs_shard_server2</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br><span class="line"></span><br><span class="line">  rs_mongos_server:</span><br><span class="line">    image: mongo:<span class="number">3.4</span></span><br><span class="line">    command:   --configdb rs-config-server/<span class="number">192.168</span><span class="number">.1</span><span class="number">.125</span>:<span class="number">10021</span>,<span class="number">192.168</span><span class="number">.1</span><span class="number">.126</span>:<span class="number">10021</span>,<span class="number">192.168</span><span class="number">.1</span><span class="number">.127</span>:<span class="number">10021</span>  --bind_ip_all</span><br><span class="line">    ports:</span><br><span class="line">      - <span class="string">&quot;10011:27017&quot;</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /database/vol/key.file:/data/mongodb/key.file</span><br><span class="line">      - /database/vol/mongos/config:/etc/mongod</span><br><span class="line">      - /database/vol/mongos/db:/data/db</span><br><span class="line">    entrypoint: mongos</span><br><span class="line">    restart:</span><br><span class="line">      always</span><br><span class="line">    container_name:</span><br><span class="line">      rs_mongos_server</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br></pre></td></tr></table></figure>
<h3 id="启动-docker-compose-up-d"><a href="#启动-docker-compose-up-d" class="headerlink" title="启动 docker-compose up -d"></a>启动 docker-compose up -d</h3><h1 id="添加副本集"><a href="#添加副本集" class="headerlink" title="添加副本集"></a>添加副本集</h1><h2 id="配置服务器"><a href="#配置服务器" class="headerlink" title="配置服务器"></a>配置服务器</h2><p>连接任意一个节点 mongo –host 192.168.1.125 –port 10021</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">rs.initiate(&#123;</span><br><span class="line">		    _id: <span class="string">&quot;rs-config-server&quot;</span>,</span><br><span class="line">		    configsvr: true,</span><br><span class="line">		    members: [</span><br><span class="line">		    	&#123; _id : <span class="number">0</span>, host : <span class="string">&quot;192.168.1.125:10021&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">1</span>, host : <span class="string">&quot;192.168.1.126:10021&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">2</span>, host : <span class="string">&quot;192.168.1.127:10021&quot;</span> &#125;,</span><br><span class="line">		    ]</span><br><span class="line">		&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="分片1"><a href="#分片1" class="headerlink" title="分片1"></a>分片1</h2><p>连接任意一个节点 mongo –host 192.168.1.125 –port 10031</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">rs.initiate(&#123;</span><br><span class="line">		    _id: <span class="string">&quot;rs-shard1-server&quot;</span>,</span><br><span class="line">		    members: [</span><br><span class="line">		    	&#123; _id : <span class="number">0</span>, host : <span class="string">&quot;192.168.1.125:10031&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">1</span>, host : <span class="string">&quot;192.168.1.126:10031&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">2</span>, host : <span class="string">&quot;192.168.1.127:10031&quot;</span> &#125;,</span><br><span class="line">		    ]</span><br><span class="line">		&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="分片2"><a href="#分片2" class="headerlink" title="分片2"></a>分片2</h2><p>连接任意一个节点 mongo –host 192.168.1.125 –port 10041</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">rs.initiate(&#123;</span><br><span class="line">		    _id: <span class="string">&quot;rs-shard2-server&quot;</span>,</span><br><span class="line">		    members: [</span><br><span class="line">		    	&#123; _id : <span class="number">0</span>, host : <span class="string">&quot;192.168.1.125:10041&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">1</span>, host : <span class="string">&quot;192.168.1.126:10041&quot;</span> &#125;,</span><br><span class="line">		        &#123; _id : <span class="number">2</span>, host : <span class="string">&quot;192.168.1.127:10041&quot;</span> &#125;,</span><br><span class="line">		    ]</span><br><span class="line">		&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="配置mongos"><a href="#配置mongos" class="headerlink" title="配置mongos"></a>配置mongos</h2><p>确保mongos服务起来之后，连接到192.168.1.125:10011执行以下命令添加分片服务器信息</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sh.addShard(<span class="string">&quot;rs-shard1-server/192.168.1.125:10031,192.168.1.126:10031,192.168.1.127:10031&quot;</span>)</span><br><span class="line">sh.addShard(<span class="string">&quot;rs-shard2-server/192.168.1.125:10041,192.168.1.126:10041,192.168.1.127:10041&quot;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="添加用户认证"><a href="#添加用户认证" class="headerlink" title="添加用户认证"></a>添加用户认证</h2><p>连接任意的mongos</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br><span class="line"></span><br><span class="line">db.createUser(</span><br><span class="line">		    &#123;</span><br><span class="line">		        user:<span class="string">&quot;root&quot;</span>,</span><br><span class="line">		        pwd:<span class="string">&quot;shiye1805A&quot;</span>,</span><br><span class="line">		        roles:[&#123;role:<span class="string">&quot;root&quot;</span>,db:<span class="string">&quot;admin&quot;</span>&#125;]</span><br><span class="line">		    &#125;</span><br><span class="line">		)</span><br></pre></td></tr></table></figure>
<h2 id="验证是否创建成功"><a href="#验证是否创建成功" class="headerlink" title="验证是否创建成功"></a>验证是否创建成功</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.auth(<span class="string">&#x27;root&#x27;</span>,<span class="string">&#x27;shiye1805A&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>返回值 1 成功</p>
<p>将配置文件的用户认证全部打开 security:</p>
<p>重启容器 注意： 保证key.file 权限为 999</p>
<h2 id="容器-操作"><a href="#容器-操作" class="headerlink" title="容器 操作"></a>容器 操作</h2><h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><p>docker rm -f rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2</p>
<h3 id="停止"><a href="#停止" class="headerlink" title="停止"></a>停止</h3><p>docker stop rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2</p>
<h3 id="重启"><a href="#重启" class="headerlink" title="重启"></a>重启</h3><p>docker restart rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2</p>
<h3 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h3><p>docker start rs_mongos_server rs_config_server rs_shard_server1 rs_shard_server2</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_43886133/article/details/95332931">link</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/yufei_java/article/details/103704582">docker-compose搭建mongodb分片集群及安全身份认证（实战）</a></p>
<p> 最近由于项目中设计中有使用mongodb，具体mongodb的优点我就不多说。这篇文章主要是分享下我通过docker-compose搭建mongodb分片集群，并实现安全身份认证访问(mongodb安装后默认是不需要用户名和密码访问的)。</p>
<h2 id="下面是我配置的docker-compose-yml文件："><a href="#下面是我配置的docker-compose-yml文件：" class="headerlink" title="下面是我配置的docker-compose.yml文件："></a>下面是我配置的docker-compose.yml文件：</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">&#x27;2&#x27;</span></span><br><span class="line">services:</span><br><span class="line">  shard_server01:</span><br><span class="line">    container_name: shard_server01</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.11</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27018</span>:<span class="number">27018</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --shardsvr --bind_ip_all</span><br><span class="line">    restart: always</span><br><span class="line">    depends_on:</span><br><span class="line">      - rs_config_server01</span><br><span class="line">      - rs_config_server02</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br><span class="line">  shard_server02:</span><br><span class="line">    container_name: shard_server02</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.12</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27028</span>:<span class="number">27018</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --shardsvr --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --bind_ip_all --auth</span><br><span class="line">    restart: always</span><br><span class="line">    depends_on:</span><br><span class="line">      - rs_config_server01</span><br><span class="line">      - rs_config_server02</span><br><span class="line"><span class="comment"># 配置服务器集群两个节点（mongodb3.4之后的版本需要两个config_server）</span></span><br><span class="line">  rs_config_server01:</span><br><span class="line">    container_name: rs_config_server01</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.13</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27019</span>:<span class="number">27019</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --configsvr --replSet <span class="string">&quot;rs_config_server&quot;</span> --bind_ip_all</span><br><span class="line">    restart: always</span><br><span class="line"> </span><br><span class="line">  rs_config_server02:</span><br><span class="line">    container_name: rs_config_server02</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.14</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27029</span>:<span class="number">27019</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --configsvr --replSet <span class="string">&quot;rs_config_server&quot;</span> --bind_ip_all</span><br><span class="line">    restart: always</span><br><span class="line"> </span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line"><span class="comment"># 路由节点mongos</span></span><br><span class="line">  mongos:</span><br><span class="line">    container_name: mongos</span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.15</span></span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27017</span>:<span class="number">27017</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    entrypoint: mongos</span><br><span class="line">    command: --configdb rs_config_server/<span class="number">192.168</span><span class="number">.1</span><span class="number">.13</span>:<span class="number">27019</span>,<span class="number">192.168</span><span class="number">.1</span><span class="number">.14</span>:<span class="number">27019</span> --bind_ip_all</span><br><span class="line">    depends_on:</span><br><span class="line">      - shard_server01</span><br><span class="line">      - shard_server02</span><br><span class="line"> </span><br><span class="line">networks:</span><br><span class="line">    mongo:</span><br><span class="line">        driver: bridge</span><br><span class="line">        ipam:</span><br><span class="line">            config:</span><br><span class="line">                - subnet: <span class="number">192.168</span><span class="number">.1</span><span class="number">.10</span>/<span class="number">24</span></span><br></pre></td></tr></table></figure>
<h3 id="注意：目前是没有增加安全身份认证的。"><a href="#注意：目前是没有增加安全身份认证的。" class="headerlink" title="注意：目前是没有增加安全身份认证的。"></a>注意：目前是没有增加安全身份认证的。</h3><h2 id="使用docker-compose启动mongo集群"><a href="#使用docker-compose启动mongo集群" class="headerlink" title="使用docker-compose启动mongo集群"></a>使用docker-compose启动mongo集群</h2><p>docker-compose up -d</p>
<h3 id="配置服务器设置（config-server）"><a href="#配置服务器设置（config-server）" class="headerlink" title="配置服务器设置（config_server）"></a>配置服务器设置（config_server）</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it rs_config_server01 /<span class="built_in">bin</span>/bash</span><br><span class="line">mongo --host localhost --port <span class="number">27019</span></span><br><span class="line">rs.initiate(&#123;</span><br><span class="line">    _id: <span class="string">&quot;rs_config_server&quot;</span>,</span><br><span class="line">    configsvr: true,</span><br><span class="line">    members: [</span><br><span class="line">        &#123; _id : <span class="number">0</span>, host : <span class="string">&quot;192.168.1.13:27019&quot;</span> &#125;,</span><br><span class="line">        &#123; _id : <span class="number">1</span>, host : <span class="string">&quot;192.168.1.14:27019&quot;</span> &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>配置路由mongos服务</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it mongos /<span class="built_in">bin</span>/bash</span><br><span class="line">mongo --port <span class="number">27017</span></span><br></pre></td></tr></table></figure>
<p>将分片集群添加到mongos中</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sh.addShard(<span class="string">&quot;192.168.1.11:27018&quot;</span>)</span><br><span class="line">sh.addShard(<span class="string">&quot;192.168.1.12:27018&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>到目前为止，mongodb分片集群已经搭建完毕。但是mongdb默认是无需账户即可直接访问。故，若是需要增加账号和密码，并强制需要输入正确的账户和密码才能登陆的话，看下文。</p>
<h2 id="1、创建mongdb的账户和密码"><a href="#1、创建mongdb的账户和密码" class="headerlink" title="1、创建mongdb的账户和密码"></a>1、创建mongdb的账户和密码</h2><p>进入mongos路由服务</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it mongos /<span class="built_in">bin</span>/bash</span><br><span class="line">mongo --port <span class="number">27017</span></span><br></pre></td></tr></table></figure>
<p> 切换到admin库，创建用户root</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br><span class="line"> </span><br><span class="line">db.createUser(</span><br><span class="line">		    &#123;</span><br><span class="line">		        user:<span class="string">&quot;root&quot;</span>,</span><br><span class="line">		        pwd:<span class="string">&quot;123456&quot;</span>,</span><br><span class="line">		        roles:[&#123;role:<span class="string">&quot;root&quot;</span>,db:<span class="string">&quot;admin&quot;</span>&#125;]</span><br><span class="line">		    &#125;</span><br><span class="line">		)</span><br></pre></td></tr></table></figure>
<h2 id="2、生成mongo节点之前通讯认证文件（key-file）"><a href="#2、生成mongo节点之前通讯认证文件（key-file）" class="headerlink" title="2、生成mongo节点之前通讯认证文件（key.file）"></a>2、生成mongo节点之前通讯认证文件（key.file）</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">openssl rand -base64 <span class="number">741</span> &gt; key.file</span><br><span class="line">chmod <span class="number">600</span> key.file</span><br><span class="line">chown <span class="number">999</span> key.file </span><br></pre></td></tr></table></figure>
<h2 id="3、将key-file挂载docker容器里面，启动命令指定key-file，并增加需要认证-–auth"><a href="#3、将key-file挂载docker容器里面，启动命令指定key-file，并增加需要认证-–auth" class="headerlink" title="3、将key.file挂载docker容器里面，启动命令指定key.file，并增加需要认证(–auth)"></a>3、将key.file挂载docker容器里面，启动命令指定key.file，并增加需要认证(–auth)</h2><p>  增加安全认证之后的docker-compose .yml文件如下</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">version: <span class="string">&#x27;2&#x27;</span></span><br><span class="line">services:</span><br><span class="line">  shard_server01:</span><br><span class="line">    container_name: shard_server01</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.11</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27018</span>:<span class="number">27018</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/shard_server01/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --shardsvr --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --bind_ip_all --auth</span><br><span class="line">    restart: always</span><br><span class="line">    depends_on:</span><br><span class="line">      - rs_config_server01</span><br><span class="line">      - rs_config_server02</span><br><span class="line">    ulimits:</span><br><span class="line">      nofile:</span><br><span class="line">        soft: <span class="number">300000</span></span><br><span class="line">        hard: <span class="number">300000</span></span><br><span class="line">  shard_server02:</span><br><span class="line">    container_name: shard_server02</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.12</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27028</span>:<span class="number">27018</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/shard_server02/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --shardsvr --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --bind_ip_all --auth</span><br><span class="line">    restart: always</span><br><span class="line">    depends_on:</span><br><span class="line">      - rs_config_server01</span><br><span class="line">      - rs_config_server02</span><br><span class="line"><span class="comment"># 配置服务器集群两个节点（mongodb3.4之后的版本需要两个config_server）</span></span><br><span class="line">  rs_config_server01:</span><br><span class="line">    container_name: rs_config_server01</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.13</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27019</span>:<span class="number">27019</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server01/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --configsvr --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --replSet <span class="string">&quot;rs_config_server&quot;</span> --bind_ip_all --auth</span><br><span class="line">    restart: always</span><br><span class="line"> </span><br><span class="line">  rs_config_server02:</span><br><span class="line">    container_name: rs_config_server02</span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.14</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27029</span>:<span class="number">27019</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/rs_config_server02/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    command: --configsvr --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --replSet <span class="string">&quot;rs_config_server&quot;</span> --bind_ip_all --auth</span><br><span class="line">    restart: always</span><br><span class="line"> </span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line"><span class="comment"># 路由节点mongos</span></span><br><span class="line">  mongos:</span><br><span class="line">    container_name: mongos</span><br><span class="line">    networks:</span><br><span class="line">      mongo:</span><br><span class="line">        ipv4_address: <span class="number">192.168</span><span class="number">.1</span><span class="number">.15</span></span><br><span class="line">    image: mongo:<span class="number">3.6</span></span><br><span class="line">    ports:</span><br><span class="line">      - <span class="number">27017</span>:<span class="number">27017</span></span><br><span class="line">    volumes:</span><br><span class="line">      - /data/docker/mongos/data/data/db:/data/db</span><br><span class="line">      - /data/docker/mongos/data/data/configdb:/data/configdb</span><br><span class="line">      - /data/docker/mongos/data/data/backup:/data/backup</span><br><span class="line">      - /data/docker/mongos/data/mongod.conf:/etc/mongod.conf</span><br><span class="line">      - /data/docker/mongos/data/key.file:/etc/key.file</span><br><span class="line">    entrypoint: mongos</span><br><span class="line">    command: --configdb rs_config_server/<span class="number">192.168</span><span class="number">.1</span><span class="number">.13</span>:<span class="number">27019</span>,<span class="number">192.168</span><span class="number">.1</span><span class="number">.14</span>:<span class="number">27019</span> --keyFile <span class="string">&quot;/etc/key.file&quot;</span> --bind_ip_all --auth</span><br><span class="line">    depends_on:</span><br><span class="line">      - shard_server01</span><br><span class="line">      - shard_server02</span><br><span class="line"> </span><br><span class="line">networks:</span><br><span class="line">    mongo:</span><br><span class="line">        driver: bridge</span><br><span class="line">        ipam:</span><br><span class="line">            config:</span><br><span class="line">                - subnet: <span class="number">192.168</span><span class="number">.1</span><span class="number">.10</span>/<span class="number">24</span></span><br></pre></td></tr></table></figure>
<h2 id="4、重启docker-compose"><a href="#4、重启docker-compose" class="headerlink" title="4、重启docker-compose"></a>4、重启docker-compose</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker-compose down</span><br><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure>
<p>到此，增加安全登录已经配置完毕。若不使用账号和密码访问结果如下：</p>

    </div>

    
    
    
        <div class="reward-container">
  <div>Thanks.</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    Donate
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="Hory Skone WeChat Pay">
        <p>WeChat Pay</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>Post author:  </strong>Hory Skone
  </li>
  <li class="post-copyright-link">
    <strong>Post link: </strong>
    <a href="http://blog.hory-ai.com/2020/07/30/%E5%A4%9A%E6%9C%BA%E7%89%88__docker-compose%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4%E8%AF%A6%E7%BB%86%E6%AD%A5%E9%AA%A4/" title="多机版———docker-compose搭建MongoDB 3.4 分片集群详细步骤">http://blog.hory-ai.com/2020/07/30/多机版__docker-compose搭建MongoDB分片集群详细步骤/</a>
  </li>
  <li class="post-copyright-license">
    <strong>Copyright Notice:  </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> unless stating additionally.
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/docker/" rel="tag"># docker</a>
              <a href="/tags/mongo/" rel="tag"># mongo</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/07/30/%E5%8D%95%E6%9C%BA%E7%89%88%E2%80%94%E2%80%94docker%E6%90%AD%E5%BB%BAMongoDB%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4/" rel="prev" title="单机版——docker搭建MongoDB分片集群">
      <i class="fa fa-chevron-left"></i> 单机版——docker搭建MongoDB分片集群
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/07/31/Docker%E5%AE%B9%E5%99%A8%E5%91%BD%E4%BB%A4%E6%95%B4%E7%90%86/" rel="next" title="Docker容器命令整理">
      Docker容器命令整理 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%89%E5%8F%B0-%E6%8C%89%E9%A1%BA%E5%BA%8F%E6%89%A7%E8%A1%8C"><span class="nav-number">1.</span> <span class="nav-text">服务器三台  按顺序执行</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95"><span class="nav-number">2.</span> <span class="nav-text">创建目录</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vi-mongod-conf"><span class="nav-number">2.1.</span> <span class="nav-text">vi mongod.conf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vi-mongos-conf"><span class="nav-number">2.2.</span> <span class="nav-text">vi mongos.conf</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95-1"><span class="nav-number">3.</span> <span class="nav-text">创建目录</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vi-first-mkdir-sh"><span class="nav-number">3.1.</span> <span class="nav-text">vi first_mkdir.sh</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%9F%E6%88%90%E8%AE%A4%E8%AF%81%E6%96%87%E4%BB%B6-root-%E7%94%A8%E6%88%B7-%E4%B8%89%E5%8F%B0%E5%85%B1%E7%94%A8%E4%B8%80%E4%B8%AA"><span class="nav-number">3.2.</span> <span class="nav-text">生成认证文件 root 用户 三台共用一个</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%86%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%94%BE%E5%88%B0%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE"><span class="nav-number">3.3.</span> <span class="nav-text">将配置文件放到指定位置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker-compose-yaml-%E5%AE%B9%E5%99%A8%E7%BC%96%E6%8E%92"><span class="nav-number">4.</span> <span class="nav-text">docker-compose.yaml 容器编排</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker-compose-%E6%96%87%E4%BB%B6"><span class="nav-number">5.</span> <span class="nav-text">docker-compose 文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8-docker-compose-up-d"><span class="nav-number">5.1.</span> <span class="nav-text">启动 docker-compose up -d</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E5%89%AF%E6%9C%AC%E9%9B%86"><span class="nav-number"></span> <span class="nav-text">添加副本集</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-number">1.</span> <span class="nav-text">配置服务器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E7%89%871"><span class="nav-number">2.</span> <span class="nav-text">分片1</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E7%89%872"><span class="nav-number">3.</span> <span class="nav-text">分片2</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEmongos"><span class="nav-number">4.</span> <span class="nav-text">配置mongos</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E8%AE%A4%E8%AF%81"><span class="nav-number">5.</span> <span class="nav-text">添加用户认证</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%AA%8C%E8%AF%81%E6%98%AF%E5%90%A6%E5%88%9B%E5%BB%BA%E6%88%90%E5%8A%9F"><span class="nav-number">6.</span> <span class="nav-text">验证是否创建成功</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8-%E6%93%8D%E4%BD%9C"><span class="nav-number">7.</span> <span class="nav-text">容器 操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4"><span class="nav-number">7.1.</span> <span class="nav-text">删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%81%9C%E6%AD%A2"><span class="nav-number">7.2.</span> <span class="nav-text">停止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%8D%E5%90%AF"><span class="nav-number">7.3.</span> <span class="nav-text">重启</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8"><span class="nav-number">7.4.</span> <span class="nav-text">启动</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%8B%E9%9D%A2%E6%98%AF%E6%88%91%E9%85%8D%E7%BD%AE%E7%9A%84docker-compose-yml%E6%96%87%E4%BB%B6%EF%BC%9A"><span class="nav-number">8.</span> <span class="nav-text">下面是我配置的docker-compose.yml文件：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F%EF%BC%9A%E7%9B%AE%E5%89%8D%E6%98%AF%E6%B2%A1%E6%9C%89%E5%A2%9E%E5%8A%A0%E5%AE%89%E5%85%A8%E8%BA%AB%E4%BB%BD%E8%AE%A4%E8%AF%81%E7%9A%84%E3%80%82"><span class="nav-number">8.1.</span> <span class="nav-text">注意：目前是没有增加安全身份认证的。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8docker-compose%E5%90%AF%E5%8A%A8mongo%E9%9B%86%E7%BE%A4"><span class="nav-number">9.</span> <span class="nav-text">使用docker-compose启动mongo集群</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%AE%BE%E7%BD%AE%EF%BC%88config-server%EF%BC%89"><span class="nav-number">9.1.</span> <span class="nav-text">配置服务器设置（config_server）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81%E5%88%9B%E5%BB%BAmongdb%E7%9A%84%E8%B4%A6%E6%88%B7%E5%92%8C%E5%AF%86%E7%A0%81"><span class="nav-number">10.</span> <span class="nav-text">1、创建mongdb的账户和密码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81%E7%94%9F%E6%88%90mongo%E8%8A%82%E7%82%B9%E4%B9%8B%E5%89%8D%E9%80%9A%E8%AE%AF%E8%AE%A4%E8%AF%81%E6%96%87%E4%BB%B6%EF%BC%88key-file%EF%BC%89"><span class="nav-number">11.</span> <span class="nav-text">2、生成mongo节点之前通讯认证文件（key.file）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3%E3%80%81%E5%B0%86key-file%E6%8C%82%E8%BD%BDdocker%E5%AE%B9%E5%99%A8%E9%87%8C%E9%9D%A2%EF%BC%8C%E5%90%AF%E5%8A%A8%E5%91%BD%E4%BB%A4%E6%8C%87%E5%AE%9Akey-file%EF%BC%8C%E5%B9%B6%E5%A2%9E%E5%8A%A0%E9%9C%80%E8%A6%81%E8%AE%A4%E8%AF%81-%E2%80%93auth"><span class="nav-number">12.</span> <span class="nav-text">3、将key.file挂载docker容器里面，启动命令指定key.file，并增加需要认证(–auth)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4%E3%80%81%E9%87%8D%E5%90%AFdocker-compose"><span class="nav-number">13.</span> <span class="nav-text">4、重启docker-compose</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Hory Skone</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horysk" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horysk" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:admin@horysk.com" title="E-Mail → mailto:admin@horysk.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://www.hory-ai.com/" title="HoryAI → http:&#x2F;&#x2F;www.hory-ai.com" rel="noopener" target="_blank"><i class="fab fa-google fa-fw"></i>HoryAI</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/sirobot" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;sirobot" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/AI_HH" title="ZhiHu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;AI_HH" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>ZhiHu</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.kaggle.com/" title="Kaggle → https:&#x2F;&#x2F;www.kaggle.com&#x2F;" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Kaggle</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://tianchi.aliyun.com/competition/gameList/activeList" title="TianChi → https:&#x2F;&#x2F;tianchi.aliyun.com&#x2F;competition&#x2F;gameList&#x2F;activeList" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>TianChi</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://works.yangerxiao.com/honeyed-words-generator" title="土情话 → https:&#x2F;&#x2F;works.yangerxiao.com&#x2F;honeyed-words-generator" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>土情话</a>
      </span>
  </div>



      </div>

      
      <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
      <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
      <div class="widget-wrap">
          <h3 class="widget-title">Tag Cloud</h3>
          <div id="myCanvasContainer" class="widget tagcloud">
              <canvas width="250" height="250" id="resCanvas" style="width:100%">
                  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/BI/" rel="tag">BI</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Centos/" rel="tag">Centos</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DNS/" rel="tag">DNS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dapp/" rel="tag">Dapp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Docker/" rel="tag">Docker</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hack/" rel="tag">Hack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HyperLedger-Fabric/" rel="tag">HyperLedger Fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hyperledger-Fabric/" rel="tag">Hyperledger Fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Iftop/" rel="tag">Iftop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ML/" rel="tag">ML</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MTProxy/" rel="tag">MTProxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mongo/" rel="tag">Mongo</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Network/" rel="tag">Network</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pyppeteer/" rel="tag">Pyppeteer</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tools/" rel="tag">Tools</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/VPN/" rel="tag">VPN</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vnc/" rel="tag">Vnc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/baostock/" rel="tag">baostock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/block-chain/" rel="tag">block chain</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/centos-xfce-vnc/" rel="tag">centos-xfce-vnc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/" rel="tag">docker</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fabric/" rel="tag">fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/horysk/" rel="tag">horysk</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/k8s/" rel="tag">k8s</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/" rel="tag">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongo/" rel="tag">mongo</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pyppeteer/" rel="tag">pyppeteer</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/quant/" rel="tag">quant</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/stock/" rel="tag">stock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tushare/" rel="tag">tushare</a><span class="tag-list-count">1</span></li></ul>
              </canvas>
          </div>
      </div>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">null </a>
  </div>

<div class="copyright">
  
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hory Skone</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">275k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">4:10</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  
  <script color='255,255,255' opacity='0.6' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('https://cdn.jsdelivr.net/npm/valine@1.3.9/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : '2ASH47v4VIRijd91HeEi3pws-gzGzoHsz',
      appKey     : 'ojRfR89cSV614pgJFT36lFFB',
      placeholder: "昵称填写qq可以显示qq头像和昵称哦~",
      avatar     : 'monsterid',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : '',
      requiredFields: ['nick','mail'] 
    });
  }, window.Valine);
});
</script>

  <!-- 页面点击特效 -->
  <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
  <script type="text/javascript" src="/js/click-word.js"></script>
  
  <script type="text/javascript"
  color="255,255,255" opacity='0.7' zIndex="-2" count="500" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/z16.model.json"},"display":{"position":"right","width":300,"height":600},"mobile":{"show":true}});</script></body>
</html>
